package com.theone.pay.view;

import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.theone.pay.R;
import com.theone.pay.model.PayLog;

/**
 * 支付日志查询的
 * 查询条件弹出窗口
 */
public class PayLogQueryPopupWindow {
    private View rootView;//根视图
    private View clickView;//点击按钮的视图
    private View popView;//弹出窗口的视图
    private Handler mHandler;
    //弹出窗口
    PopupWindow window;


    EditText et_orderid;
    Button but_sure ;
    Button but_reset ;
    RadioGroup rg_payState;
    RadioGroup rg_payType ;
    RadioGroup rg_createtime ;

    public PayLogQueryPopupWindow(View rootView,View clickView,View popView,Handler mHandler){
        this.rootView = rootView;
        this.clickView = clickView;
        this.popView = popView;
        this.mHandler= mHandler;
        initView();
    }

    private void initView(){
        et_orderid = popView.findViewById(R.id.et_orderid);
        but_sure = popView.findViewById(R.id.but_sure);
        but_reset = popView.findViewById(R.id.but_reset);
        rg_payState = popView.findViewById(R.id.rg_payState);
        rg_payType = popView.findViewById(R.id.rg_payType);
        rg_createtime = popView.findViewById(R.id.rg_createtime);

        //注册事件
        but_reset.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            //
                                            PayLog.saveCurrQueryPayLog(new PayLog());
                                            initQueryValue();
                                        }
                                    }
        );
        but_sure.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            PayLog log = new PayLog();
                                            if(et_orderid.getText()!=null){
                                                log.setOrderid(et_orderid.getText().toString());
                                            }
                                            if(rg_createtime.getCheckedRadioButtonId()==R.id.rb_createtime_1){
                                                log.setCreatetime("1");
                                            }
                                            if(rg_createtime.getCheckedRadioButtonId()==R.id.rb_createtime_2){
                                                log.setCreatetime("2");
                                            }
                                            if(rg_payState.getCheckedRadioButtonId()==R.id.rb_payState_0){
                                                log.setPayState(0);
                                            }
                                            if(rg_payState.getCheckedRadioButtonId()==R.id.rb_payState_1){
                                                log.setPayState(1);
                                            }
                                            if(rg_payType.getCheckedRadioButtonId()==R.id.rb_payType_1){
                                                log.setPayType(1);
                                            }
                                            if(rg_payType.getCheckedRadioButtonId()==R.id.rb_payType_2){
                                                log.setPayType(2);
                                            }
                                            PayLog.saveCurrQueryPayLog(log);
                                            mHandler.obtainMessage(11, "重新查询").sendToTarget();
                                            window.dismiss();
                                        }
                                    }
        );
    }


    //初始化参数
    public void initQueryValue(){
        PayLog ql = PayLog.getCurrQueryPayLog();
        if(ql!=null){
            if(ql.getOrderid()==null){
                et_orderid.setText("");
            }else{
                et_orderid.setText(ql.getOrderid());
            }
            if(ql.getCreatetime()==null){
                rg_createtime.clearCheck();
            }else{
                if(ql.getCreatetime().equals("1")){
                    rg_createtime.check(R.id.rb_createtime_1);
                }
                if(ql.getCreatetime().equals("2")){
                    rg_createtime.check(R.id.rb_createtime_2);
                }
            }
            if(ql.getPayState()==null){
                rg_payState.clearCheck();
            }else{
                if(ql.getPayState()==0){
                    rg_payState.check(R.id.rb_payState_0);
                }
                if(ql.getPayState()==1){
                    rg_payState.check(R.id.rb_payState_1);
                }
            }
            if(ql.getPayType()==null){
                rg_payType.clearCheck();
            }else{
                if(ql.getPayType()==1){
                    rg_payType.check(R.id.rb_payType_1);
                }
                if(ql.getPayType()==2){
                    rg_payType.check(R.id.rb_payType_2);
                }
            }
        }
    }



    public void show(){
        //位置计算定位
        int[] location = new int[2];
        clickView.getLocationOnScreen(location);
        int width = rootView.getWidth()*2/3;
        int height = rootView.getHeight()*2/3;
        window=new PopupWindow(popView,  width,  height, true);
        // 设置PopupWindow的背景
        //window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        window.setTouchable(true);
        // 显示PopupWindow，其中：
        // 第一个参数是PopupWindow的锚点，第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
        window.showAtLocation(rootView, Gravity.NO_GRAVITY,location[0]-width-10,location[1]);

    }


}
